package com.one.group.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.model.dto.WorkAssignmentAddDto;
import com.one.group.model.dto.WorkAssignmentQueryDto;
import com.one.group.model.dto.WorkAssignmentReceiptDto;
import com.one.group.model.dto.WorkAssignmentUpdateDto;
import com.one.group.model.vo.WorkAssignmentListVo;
import com.one.group.result.Result;
import com.one.group.service.WorkAssignmentService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;//

/**
 * @author 
 * @date 
 * @description 派工管理控制器
 */
@RestController
@RequestMapping("/workAssignment")
@Tag(name = "派工管理")
@Slf4j
public class WorkAssignmentController {

    @Autowired
    private WorkAssignmentService workAssignmentService;

    /**
     * 获取派工列表（分页）
     * @param workAssignmentQueryDto 查询参数
     * @return 派工列表
     */
    @PostMapping("/list")
    @Operation(summary = "获取派工列表")
    public Result list(@RequestBody WorkAssignmentQueryDto workAssignmentQueryDto) {
        // 设置分页条件
        PageHelper.startPage(workAssignmentQueryDto.getPage(), workAssignmentQueryDto.getLimit());
        // 查询数据
        List<WorkAssignmentListVo> list = workAssignmentService.findWorkAssignmentByQueryDto(workAssignmentQueryDto);
        
        // 封装分页结果
        PageInfo<WorkAssignmentListVo> pageInfo = new PageInfo<>(list);
        
        return ResultUtil.success(pageInfo);
    }

    /**
     * 派工
     * @param workAssignmentAddDto 添加参数
     * @return 是否成功
     */
    @PostMapping("/add")
    @Operation(summary = "添加派工信息")
    public Result add(@RequestBody WorkAssignmentAddDto workAssignmentAddDto) {
        if (workAssignmentService.insertWorkAssignment(workAssignmentAddDto)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "派工失败，请稍后重试");
        }
    }

    /**
     * 更新派工信息
     * @param workAssignmentUpdateDto 更新参数
     * @return 是否成功
     */
    @PostMapping("/update")
    @Operation(summary = "更新派工信息")
    public Result update(@RequestBody WorkAssignmentUpdateDto workAssignmentUpdateDto) {
        if (workAssignmentService.updateWorkAssignment(workAssignmentUpdateDto)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "更新失败");
        }
    }

    /**
     * 删除派工信息
     * @param id 派工ID
     * @return 是否成功
     */
    @GetMapping("/delete/{id}")
    @Operation(summary = "删除派工信息")
    public Result delete(@PathVariable Long id) {
        if (workAssignmentService.deleteWorkAssignment(id)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "删除失败");
        }
    }

    /**
     * 将派工单状态更新为已完成
     * @param id 派工单ID
     * @return 是否成功
     */
    @PostMapping("/updateToCompleted/{id}")
    @Operation(summary = "更新派工单状态为已完成")
    public Result updateToCompleted(@PathVariable Long id) {
        if (workAssignmentService.updateToCompleted(id)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "更新状态失败");
        }
    }
    
    /**
     * 根据ID查询派工信息
     * @param id 派工ID
     * @return 派工信息
     */
    @GetMapping("/find/{id}")
    @Operation(summary = "根据ID查询派工信息")
    public Result findById(@PathVariable Long id) {
        return ResultUtil.success(workAssignmentService.findById(id));
    }
    
    /**
     * 派工回单
     * @param receiptDto 回单参数
     * @return 操作结果
     */
    @PostMapping("/receipt")
    @Operation(summary = "派工回单")
    public Result submitReceipt(@RequestBody WorkAssignmentReceiptDto receiptDto) {
        log.debug("派工回单请求参数: {}", receiptDto);
        if (workAssignmentService.submitReceipt(receiptDto)) {
            return ResultUtil.success("回单成功");
        } else {
            return ResultUtil.error(500, "回单失败，请稍后重试");
        }
    }
}